package com.xh.fma.commodity.service.impl;

import com.xh.fma.commodity.vo.Catalog2Vo;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xh.common.utils.PageUtils;
import com.xh.common.utils.Query;

import com.xh.fma.commodity.dao.CategoryDao;
import com.xh.fma.commodity.entity.CategoryEntity;
import com.xh.fma.commodity.service.CategoryService;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Override
    public List<CategoryEntity> listTree() {
        List<CategoryEntity> entities = baseMapper.selectList(null);
        //对查出的list进行过滤处理
        List<CategoryEntity> entitiesTree = entities.stream().filter(categoryEntity -> {
            return categoryEntity.getCatLevel() == 1;
        }).map(categoryEntity -> {
            categoryEntity.setChildrenCategories(getChildrenCategories(categoryEntity,entities));
            return categoryEntity;
        }).sorted((c1, c2) -> {
            return (c1.getSort() == null ? 0 : c1.getSort()) -
                    (c2.getSort() == null ? 0 : c2.getSort());
        }).collect(Collectors.toList());


        return entitiesTree;
    }

    @Override
    public List<CategoryEntity> listLeve1() {
        List<CategoryEntity> entityList = this.baseMapper.selectList(
                new QueryWrapper<CategoryEntity>().eq("parent_id", 0)
        );

        return entityList;
    }

    @Override
    public Long[] selectCategory(Long categoryId) {
        ArrayList<Long>  categories = new ArrayList<>();
        selectCategories(categoryId,categories);
        Collections.reverse(categories);

        return categories.toArray(new Long[categories.size()]);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public Map<String, List<Catalog2Vo>> getCatalogJson() {

        // 查询到整个分类信息
        List<CategoryEntity> entities =
                this.baseMapper.selectList(null);

        List<CategoryEntity> level1 = getParent_cid(entities, 0L);

        Map<String, List<Catalog2Vo>> collect = level1.stream().collect(Collectors.toMap(k -> k.getId().toString(),
                v -> {
                    List<CategoryEntity> level2 = getParent_cid(entities, v.getId());

                    List<Catalog2Vo> catalog2Vos = null;

                    if (level2 != null) {
                        catalog2Vos = level2.stream().map(item -> {
                            Catalog2Vo catalog2Vo = new Catalog2Vo();
                            catalog2Vo.setCatalog1Id(v.getId().toString());
                            catalog2Vo.setId(item.getId().toString());
                            catalog2Vo.setName(item.getName());

                            List<CategoryEntity> level3 = getParent_cid(entities, item.getId());

                            if (level3 != null) {

                                List<Catalog2Vo.Category3Vo> category3Vos = level3.stream().map(l3 -> {
                                    return new Catalog2Vo.Category3Vo(item.getId().toString(),
                                            l3.getId().toString(),
                                            l3.getName());
                                }).collect(Collectors.toList());
                                catalog2Vo.setCatalog3List(category3Vos);

                            }
                            return catalog2Vo;
                        }).collect(Collectors.toList());
                    }
                    return catalog2Vos;
                }
        ));

        return collect;
    }

    // 创建一个方法 输入当前分类级别 可以查询下一级分类内容
    public List<CategoryEntity> getParent_cid(List<CategoryEntity> entities, Long parentId) {
        // 使用stream的filter方法过滤
        List<CategoryEntity> collect = entities.stream().filter(item ->
            item.getParentId().equals(parentId)
        ).collect(Collectors.toList());

        return collect;
    }

    // 创建一个方法  对类进行挂载分级
    public List<CategoryEntity> getChildrenCategories(CategoryEntity root, List<CategoryEntity> entities ){
        List<CategoryEntity> childrenCategories = entities.stream().filter(categoryEntity -> {
            return categoryEntity.getParentId() == root.getId();
        }).map(categoryEntity -> {
            categoryEntity.setChildrenCategories(getChildrenCategories(categoryEntity, entities));
            return categoryEntity;
        }).sorted((c1, c2) -> {
            return (c1.getSort() == null ? 0 : c1.getSort()) -
                    (c2.getSort() == null ? 0 : c2.getSort());
        }).collect(Collectors.toList());

        return childrenCategories;
    }

    public void selectCategories(Long categoryId, List<Long> categories){
        categories.add(categoryId);
        CategoryEntity entity = this.getById(categoryId);
        if (entity.getParentId() != 0){
            selectCategories(entity.getParentId(),categories);
        }
    }

}